﻿package com.powerunion.extjs.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
//import java.util.Map;

import javax.servlet.ServletException;
//import javax.servlet.SingleThreadModel;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.newsoft.util.Instance;
import com.newsoft.util.Parameter;
//implements SingleThreadModel
public class Service extends HttpServlet  {

	/**
	 * Constructor of the object.
	 */
	public Service() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml;charset=utf-8");
        PrintWriter out = response.getWriter();
    	String className	= request.getParameter("className");	//业务对象名称
    	String method 		= request.getParameter("method");		//业务对象方法
    	String methodParam = request.getParameter("methodParam");	//方法执行参数
    	String sqlflag = request.getParameter("sqlflag");
    	String str = "";
    	Convert c = new Convert();
    	Instance.printReq(request);	//打印传递的所有参数
    	Object obj = Instance.create(className);
    	
    	String sql = (String) c.getProperty(obj, "sql");
    	
    	if(sqlflag != null && !sqlflag.equals("")){
    		HashMap sqlmap = (HashMap) c.getProperty(obj, "sqlmap");
    		sql = sqlmap.get(sqlflag)+"";
    		if(sql == null || sql.equals("")){
    			System.err.print("没有配置sql语句，sqlflag="+sqlflag);
    		}
    	}
    	
    	if(sql != null){
    		Object p = new Parameter(request, sql);
    		String psql = (String) c.getProperty(p, "sql");
    		String[][] params = (String[][]) c.getProperty(p, "params");
    		int pageStart = (Integer) c.getProperty(p, "pageStart");
    		int pageSize = (Integer) c.getProperty(p, "pageSize");
    		c.invokeMethod(obj, "setSql", new Object[]{psql}); //查询语句
    		if(params != null)
    		c.invokeMethod(obj, "setParams", new Object[]{params}); 		//查询参数
    		c.invokeMethod(obj, "setPageStart", new Object[]{pageStart}); 	//起始行
    		c.invokeMethod(obj, "setPageSize", new Object[]{pageSize}); 	//每页显示条数
    	}
    	if(methodParam != null){
    		c.invokeMethod(obj, "setRequest", new Object[]{request});
    	}
    	if(method != null){
    		str = (String) c.invokeMethod(obj, method);
    	}
    	out.println(str);
        out.flush();
        out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
